package com.jeff.tianti.cms.dao;

import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeff.tianti.cms.entity.Constitution;
import com.jeff.tianti.cms.dto.ConstitutionQueryDTO;
import org.apache.commons.lang3.StringUtils;

/**
 * @author NOW_GO
 * @desc ConstitutionDaoImpl类
 * @date 2019-07-11
 */

public class ConstitutionDaoImpl extends CustomBaseSqlDaoImpl implements ConstitutionDaoCustom {

    public PageModel<Constitution> queryConstitutionPage(ConstitutionQueryDTO constitutionQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Constitution t where t.deleteFlag = 0");
        if (constitutionQueryDTO != null) {
            String title = constitutionQueryDTO.getTitle();
            if (StringUtils.isNotBlank(title)) {
                hql.append(" and t.title like :title ");
                map.put("title", "%" + title + "%");
            }
            String columnId = constitutionQueryDTO.getColumnId();
            if (StringUtils.isNotBlank(columnId)) {
                hql.append(" and t.columnId like :columnId ");
                map.put("columnId", columnId);
            }
        }
        hql.append(" order by t.createDate desc ");
        return this.queryForPageWithParams(hql.toString(), map, constitutionQueryDTO.getCurrentPage(), constitutionQueryDTO.getPageSize());
    }

    public List<Constitution> queryConstitutionList(ConstitutionQueryDTO constitutionQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Constitution t where t.deleteFlag = 0");
        if (constitutionQueryDTO != null) {
            String columnId = constitutionQueryDTO.getColumnId();
            if (StringUtils.isNotBlank(columnId)) {
                hql.append(" and t.columnId = :columnId ");
                map.put("columnId", columnId);
            }
        }
        hql.append(" order by t.createDate desc ");
        return this.queryByMapParams(hql.toString(), map);
    }

    public List queryList(ConstitutionQueryDTO constitutionQueryDTO) {
        StringBuilder hql = new StringBuilder();
        hql.append("select id,title,DATE_FORMAT(create_date,'%Y-%m-%d') as createDate ,description from constitution  where delete_flag = 0");
        if (constitutionQueryDTO != null) {
            String columnId = constitutionQueryDTO.getColumnId();
            String kewWord = constitutionQueryDTO.getKewWord();
            if (StringUtils.isNotBlank(columnId)) {
                hql.append(" and column_id = " + "'" + columnId.toString() + "'");
            }
            if (StringUtils.isNotBlank(kewWord)) {
                hql.append(" and title = " + "'" + kewWord + "'");
            }
        }
        hql.append(" order by create_date desc ");

        return this.querySqlObjects(hql.toString());
    }

    public List<Map<String, Object>> queryTitleList(String kewWord) {
        StringBuilder hql = new StringBuilder();
        hql.append("select id,title from constitution  where delete_flag = 0");
        if (!StringUtils.isEmpty(kewWord)) {
            if (StringUtils.isNotBlank(kewWord)) {
                hql.append(" and title = " + "'" + kewWord + "'");
            }
        }
        hql.append(" order by create_date desc limit 0,10 ");
        return  this.querySqlObjects(hql.toString());
    }


}